import unittest
from numbertheory import *

class UtilFuncTestCase(unittest.TestCase):

    def testGcd(self):
        self.assertEqual(gcd(108,25),1)
        self.assertEqual(gcd(24,16),8)

    def testIs_prime(self):
        primes = [239, 241, 251, 257, 263, 269, 271]
        for p in primes:
            self.assertTrue(is_prime(p))
        self.assertFalse(is_prime(340561))

    def testPrime_factorization(self):
        p = 2**3 * 5**7 * 29**2 * 43**4
        factors =  prime_factorization(p)
        self.assertEqual([(2, 3), (5, 7), (29, 2), (43, 4)],factors)
    
#############seperate line################
class FractionTestCase(unittest.TestCase):
    def setUp(self):
        self.a = Fraction(3,5)
        self.b = Fraction(1,7)

    def testAdd(self):
        c = self.a + self.b
        self.assertEqual(c,Fraction(26,35))

    def testSub(self):
        c = self.a - self.b
        self.assertEqual(c,Fraction(16,35))

        c = self.b - self.a
        self.assertEqual(c,Fraction(-16,35))

    def testMul(self):
        c = self.a * self.b
        self.assertEqual(c,Fraction(3,35))
        na = Fraction(3,2)
        nb = Fraction(2,3)
        self.assertEqual(na*nb,Fraction(1,1))

    def testDiv(self):
        c = self.a / self.b
        self.assertEqual(c,Fraction(21,5))
        c = self.b / self.a
        self.assertEqual(c,Fraction(5,21))

    def testNeg(self):
        c = -self.a
        self.assertEqual(c,Fraction(-3,5))
        
    def testInt(self):
        c = int(self.a)
        self.assertEqual(c,0)
        self.assertEqual(int(Fraction(10,3)),3)

    def testInvert(self):
        c = ~self.a
        self.assertEqual(c,Fraction(5,3))
    
    def testLt(self):
        self.assertTrue(self.a < Fraction(4,5))
        self.assertTrue(self.a < 1)

    def testLe(self):
        self.assertTrue(self.a <= Fraction(3,5))
        self.assertTrue(self.a <= 0.6)

    def testEq(self):
        self.assertTrue(self.a == Fraction(3,5))
        self.assertTrue(self.a == 0.6)

    def testNe(self):
        self.assertTrue(self.a != Fraction(4,5))
        self.assertFalse(self.a != 0.6)
        
    def testGt(self):
        self.assertTrue(self.a > self.b)
        self.assertTrue(self.a > 0.1)
        self.assertFalse(self.b > self.a)

    def testGe(self):
        self.assertTrue(self.a >= 0.6)
        self.assertTrue(self.a >= self.b)

    def testCmp(self):
        flist = sorted([self.a,self.b])
        self.assertTrue(flist[0] < flist[1])

    def testNonzero(self):
        t = Fraction(0,1)
        self.assertFalse(bool(t))
        self.assertTrue(bool(self.a))

    def testAbs(self):
        t = Fraction(2,-9)
        self.assertEqual(abs(t),Fraction(2,9))
        
if __name__=="__main__":unittest.main()
